package com.tongda.core.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;

import android.content.Context;
import android.preference.PreferenceManager;

import com.tongda.core.common.Constants;
import com.tongda.core.common.KernalApplication;
import com.tongda.core.enums.StatusCodeEnum;
import com.tongda.core.utils.CommunicationUtil;
import com.tongda.core.utils.SharedPreferencesUtils;
import com.tongda.core.vo.Employee;
import com.tongda.core.vo.MobileRspResult;

public class UserService extends BaseService {

	private static Employee employee = null;

	public static ResponseEntity<String> changePassword(
			MultiValueMap<String, Object> formData) {
		return BaseService.post("mobile/table/changePassword", formData,
				String.class);
	}

	public static void main(String[] args) {
		System.out.println();
	}

	public static int login(Context context, String username, String password,
			boolean isAutoLogin) {
		Map<Object, Object> parameters = new HashMap<Object, Object>();
		parameters.put("username", username);
		parameters.put("password", password);
		parameters.put("from", "mobile");
		CommunicationUtil.SESSION_ID = "";
		BaseService.COOKIE_STR = "";
		String temp = CommunicationUtil.post(Constants.BASE_SERVIER_URL
				+ "login", parameters);
		int resp = 3;
		if (temp != null && !temp.equals("")) {
			resp = Integer.valueOf(temp);
		}
		// 登录成功
		if (resp == 0) {
			try {
				BaseService.COOKIE_STR = Constants.COOKIE_STR_USERNAME
						+ URLEncoder.encode(username, "utf-8")
						+ Constants.COOKIE_STR_PASSWORD + password;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			SharedPreferencesUtils.setParam(context, "USER_NAME", username);
			SharedPreferencesUtils.setParam(context, "PASSWORD", password);
			SharedPreferencesUtils.setParam(context, "cookie",
					BaseService.COOKIE_STR);
			SharedPreferencesUtils
					.setParam(context, "isAutoLogin", isAutoLogin);
			SharedPreferencesUtils.setParam(context, Constants.SESSIONID,
					CommunicationUtil.SESSION_ID);
			initCurrentUser(context);
		}
		return resp;
	}

	/**
	 * 退出
	 * 
	 * @param context
	 * @param username
	 */
	public static void logout(Context context) {
		ResponseEntity<MobileRspResult> responseEntity = BaseService.postUrl("mobile/logout", MobileRspResult.class);
		if (responseEntity != null) {
			
		}
		CommunicationUtil.SESSION_ID = "";
		BaseService.COOKIE_STR = "";
		employee = null;
	}

	private static void initCurrentUser(Context context) {
		Employee employee = UserService.getEmployee(context);
		Constants.RESTAURANT_ID = employee.getRestaurantId();
		KernalApplication application = KernalApplication.getInstance();
		application.setCurrentUser(employee);
	}

	/**
	 * 得到登录用户名
	 * 
	 * @param context
	 * @param loginUsername
	 * @return
	 */
	public static String getShareLoginUsername(Context context) {
		return (String) SharedPreferencesUtils.getParam(context, "USER_NAME",
				"");
	}

	public static String getShareLoginPassword(Context context) {
		return (String) SharedPreferencesUtils
				.getParam(context, "PASSWORD", "");
	}

	public static Boolean getShareIsAutoLogin(Context context) {
		return (Boolean) SharedPreferencesUtils.getParam(context,
				"isAutoLogin", true);
	}

	/**
	 * 得到登录用户
	 * 
	 * @param loginUsername
	 * @return
	 */
	public static Employee getEmployee(Context context) {
		if (employee == null) {
			employee = getCurrentUserByLoginUsername(getShareLoginUsername(context));
		}
		return employee;
	}

	public static Employee getCurrentUserByLoginUsername(String loginUsername) {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("loginUsername", loginUsername);
		ResponseEntity<MobileRspResult> responseEntity = BaseService.get(
				"mobile/table/getCurrentUser", MobileRspResult.class, params);
		if (responseEntity != null) {
			MobileRspResult r = responseEntity.getBody();
			if (r.getStatusCode().equals(StatusCodeEnum.SUCCESS.getCode())) {
				return parseObjectToT(r.getObject(), Employee.class);
			}
		}
		return null;
	}

	public static Boolean getCurrentUserHasPermission(String expression) {
		Map<String, Object> params = new WeakHashMap<String, Object>();
		params.put("expression", expression);
		ResponseEntity<MobileRspResult> responseEntity = BaseService.get(
				"mobile/table/hasPermission", MobileRspResult.class, params);
		if (responseEntity != null) {
			MobileRspResult r = responseEntity.getBody();
			if (r.getStatusCode().equals(StatusCodeEnum.SUCCESS.getCode())) {
				return (Boolean) r.getObject();
			}
		}
		return false;
	}

	public static String getServerIp(Context context) {
		return "".equals(PreferenceManager.getDefaultSharedPreferences(context)
				.getString("server_ip", "")) ? Constants.SERVER_IP
				: PreferenceManager.getDefaultSharedPreferences(context)
						.getString("server_ip", "");
	}
}
